<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="(item,index) in hot" :key="index" @click="handleCityClick(item.name)">
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(value,key,index) in cities" :key="index"  :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list" v-for="valueItem in value" :key="valueItem.id" @click="handleCityClick(valueItem.name)">
          <div class="item border-bottom">{{valueItem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
/*
  npm install better-scroll 酷炫的拖动效果
 */
import { mapState, mapMutations } from 'vuex'
import Bscroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hot: Array,
    letter: String
  },
  methods: {
    handleCityClick (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        // console.log(element)
        this.scroll.scrollToElement(element)
      }
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl';
    .border-topbottom::after
      border-color:#ccc
    .border-topbottom::before
      border-color:#ccc
    .border-bottom::before
      border-color:#ccc
    .title
      line-height: .54rem
      background: #eee
      padding-left: .2rem
      font-size: .26rem
    .list
      position: absolute
      left: 0
      right: 0
      top: 1.58rem
      bottom: 0
      overflow: hidden
      .button-list
        padding: .1rem .6rem .1rem .1rem
      .button-list::after
        content: '.'
        height: 0px
        display: block
        clear: both
        visibility: hidden
      .button-wrapper
        width: 33.33%
        float: left
        .button
          margin: .1rem
          padding: .1rem
          text-align: center
          border: .02rem solid #ccc
      .item-list
        .item
          line-height: .76rem
          padding-left: .2rem
</style>
